package org.massdeception.content.servlets;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.massdeception.content.entities.Slide;
import org.massdeception.content.util.SlideUtils;

public class SlideServlet extends HttpServlet
{    
    private static final Logger log = Logger.getLogger(StreamSlideServlet.class.getName());
    
    private static HashMap<String, String> mimeTypes;
    
    public void init(ServletConfig config) throws ServletException
    {
        
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        try
        {
            String date = request.getRequestURI();
            
            // strip the leading slash
            date = date.substring(1, date.indexOf("."));
            
            long normDate = SlideUtils.getNormalizedDate(date, false);
            long today = SlideUtils.getNormalizedDate(new Date(), true);
            
            Slide slide = SlideUtils.getSlideByDay(normDate);
            
            //log.log(Level.WARNING, "#### current slide: "+slide.getId());
            
            // get the motd
            request.setAttribute("motd", SlideUtils.getMOTD());
            
            log.log(Level.WARNING, "MOTD: "+SlideUtils.getMOTD());
            
            // if today's slide is null, get the most recent
            if ( slide == null )
            {
                slide = SlideUtils.getMostRecentSlide(normDate);
                if ( slide == null )
                {
                    RequestDispatcher dispatcher = request.getRequestDispatcher("/template.jsp");
                    dispatcher.forward(request, response);
                    
                    return;
                }
                normDate = slide.getPublishDate();
            }

            Slide yesterday = SlideUtils.getPreviousSlide(normDate);
            Slide tomorrow = null;
            //log.log(Level.WARNING, "#### norm date: "+normDate+", today: "+today);
            // avoid giving away any future slides
            if ( today > (normDate+1) )
                tomorrow = SlideUtils.getNextSlide(normDate);
            
            request.setAttribute("today", slide);
            request.setAttribute("yesterday", yesterday);
            request.setAttribute("tomorrow", tomorrow);
                        
            RequestDispatcher dispatcher = request.getRequestDispatcher("/template.jsp");
            dispatcher.forward(request, response);

            return;
        }
        catch (Throwable t)
        {
            log.log(Level.WARNING, "Could not find slide.", t);
            response.sendError(404, "Could not find slide.");
        }
    }
}